/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package skat.collections;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import skat.Card;
import skat.Main;
import skat.games.Game;

/**
 * 
 * @author weinpau
 */
public class Hand extends ArrayList<Card> {

    @Override
    public String toString() {
        return toString(false);
    }

    @Override
    public boolean addAll(Collection<? extends Card> c) {
        boolean b = super.addAll(c);
        if (b) {
            Collections.sort(this, Main.comp);
        }
        return b;
    }

    @Override
    public boolean add(Card e) {
        boolean b = super.add(e);
        if (b) {
            Collections.sort(this, Main.comp);
        }
        return b;
    }

    public String toString(boolean lg) {
        StringBuilder sb = new StringBuilder();

        for (Card card : this) {
            if (sb.length() > 0) {
                sb.append(", ");
            }
            sb.append(card.toString(lg));
        }

        return sb.toString();
    }

    public int getPoints() {
        int p = 0;
        for (Card card : this) {
            p += card.value();
        }
        return p;
    }

    public List<Card> getFollowSuitCards(Card c, Game g) {

        if (c == null) {
            return this;
        }

        List<Card> al = new ArrayList<Card>();
        if (g.isTrumpCard(c)) {
            for (Card e : this) {
                if (g.isTrumpCard(e)) {
                    al.add(e);
                }
            }
        } else {
            for (Card e : this) {
                if (!g.isTrumpCard(e) && c.suit() == e.suit()) {
                    al.add(e);
                }
            }
        }

        return al;
    }

    public List<Card> getFittingCards(Card c, Game g) {
        if (c == null) {
            return this;
        }
        List<Card> al = getFollowSuitCards(c, g);
        if (al.isEmpty()) {
            return this;
        }

        return al;
    }
}
